package com.example.wisdomclassroomapi.common.dto;

import com.example.wisdomclassroomapi.common.enums.ExceptionInfoEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
 * 响应DTO
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseDto<T> implements Serializable {
    private String code;
    private String message;
    private T data;

    public ResponseDto() {
    }

    public static ResponseDto<Object> success() {
        ResponseDto<Object> dto = new ResponseDto<>();
        dto.code = ExceptionInfoEnum.SUCCESS.getCode();
        dto.message = ExceptionInfoEnum.SUCCESS.getMessage();
        return dto;
    }

    public static <T> ResponseDto<T> success(T data) {
        ResponseDto<T> dto = new ResponseDto<>();
        dto.code = ExceptionInfoEnum.SUCCESS.getCode();
        dto.message = ExceptionInfoEnum.SUCCESS.getMessage();
        dto.data = data;
        return dto;
    }

    public static <T> ResponseDto<T> fail(String code, String message, T data) {
        ResponseDto<T> dto = new ResponseDto<>();
        dto.code = code;
        dto.message = message;
        dto.data = data;
        return dto;
    }

    public static <T> ResponseDto<T> fail(ExceptionInfoEnum info, T data) {
        return fail(info.getCode(), info.getMessage(), data);
    }

    public static <T> ResponseDto<T> fail(String code, String message) {
        return fail(code, message, null);
    }

    public static ResponseDto<?> fail(ExceptionInfoEnum info) {
        return fail(info.getCode(), info.getMessage(), null);
    }

    public static ResponseDto<?> fail() {
        return fail(ExceptionInfoEnum.SYSTEM_EXCEPTION);
    }
}
